/*
 * Copyright © 2004-2014 chenYuan. All rights reserved.
 * @Website:wwww.jspx.net
 * @Mail:39793751@qq.com
 * @author: chenYuan , 陈原
 * @License: Jspx.net Framework Code is open source (LGPL)，Jspx.net Framework 使用LGPL 开源授权协议发布。
 * @jvm:jdk1.6+  x86/amd64
 *
 */
package com.jspx.txweb.module;

import com.jspx.boot.EnvFactory;
import com.jspx.boot.environment.EnvironmentTemplate;
import com.jspx.boot.environment.JspxConfiguration;
import com.jspx.sioc.BeanFactory;
import com.jspx.txweb.ActionInvocation;
import com.jspx.txweb.WebConfigManager;
import com.jspx.txweb.config.ActionConfig;
import com.jspx.txweb.proxy.DefaultActionInvocation;
import com.jspx.txweb.config.TXWebConfigManager;
import com.jspx.txweb.support.ActionSupport;
import com.jspx.txweb.util.TXWebUtil;
import com.jspx.utils.StringUtil;
import com.jspx.sober.exception.ValidException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * Created by IntelliJ IDEA.
 * User: 陈原
 * Date: 2007-12-20
 * Time: 15:40:12
 */
public class JspxModule
{
    public JspxModule()
    {
        
    }

    public static JspxConfiguration getBaseConfiguration()
    {
        return EnvFactory.getBaseConfiguration();
    }

    public static BeanFactory getBeanFactory()
    {
        return EnvFactory.getBeanFactory();
    }

    public static EnvironmentTemplate getEnvironmentTemplate()
    {
        return EnvFactory.getEnvironmentTemplate();
    }

    public static Object getBean(String classname)
    {
        String txweb_namespace = null;
        if (classname == null) return null;
        if (classname.contains(TXWebUtil.AT))
        {
            txweb_namespace = StringUtil.substringAfter(classname,TXWebUtil.AT);
            classname = StringUtil.substringBefore(classname,TXWebUtil.AT);
        }
        BeanFactory beanFactory = getBeanFactory();
        if (txweb_namespace == null)
            return beanFactory.getBean(classname);
        return beanFactory.getBean(classname,txweb_namespace);
    }

    public static Object getBean(String name, String namespace)
    {
        BeanFactory beanFactory = getBeanFactory();
        return beanFactory.getBean(name, namespace);
    }

    public static ActionSupport getAction(HttpServletRequest request, HttpServletResponse response, String namePart) throws  ValidException
    {
        String namespace = TXWebUtil.getNamespace(request.getServletPath());
        return getAction(request, response, namePart, namespace);
    }

    public static ActionSupport getAction(HttpServletRequest request, HttpServletResponse response, String namePart, String namespace) throws ValidException
    {
        if (StringUtil.isNULL(namespace)) namespace = StringUtil.empty;
        ///////////////////////////////////环境参数 begin


        Map<String, Object> envParams = TXWebUtil.createEnvironment();
        envParams.put(ActionSupport.Key_ActionName, namePart);
        envParams.put(ActionSupport.Key_Namespace, namespace);
        envParams.put(ActionSupport.Key_RealPath, request.getServletPath());
        envParams.put(ActionSupport.ContentType, "text/html; charset=" + request.getCharacterEncoding());
        ///////////////////////////////////环境参数 end
        ////////////////////ajax begin
        WebConfigManager webConfigManager = TXWebConfigManager.getInstance();
        try
        {
            ActionConfig actionConfig = webConfigManager.getActionConfig(namePart, namespace, true);
            if (actionConfig == null)
            {
                envParams.clear();
                return null;
            }
            ActionInvocation actionInvocation = new DefaultActionInvocation(actionConfig, envParams,null,request,response);
            actionInvocation.initAction();
            actionInvocation.invoke();
            return actionInvocation.getActionProxy().getAction();
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        ////////////////////ajax end
        return null;
    }

    public static Object getAction(String name, String namespace)
    {
        BeanFactory beanFactory = getBeanFactory();
        return beanFactory.getBean(name, namespace);
    }

}